package edu.purdue.spm.util;

import java.util.Calendar;
import java.util.Date;

import android.text.format.DateUtils;
import android.text.format.Time;
import android.util.Log;

public class StopWatch {

	
	public static int getTimeInSec(){
		Calendar c = Calendar.getInstance(); 
		int seconds = c.get(Calendar.SECOND);
		return seconds;
	}
	
	public static int getTimeInMin(){
		Calendar c = Calendar.getInstance(); 
		int minutes = c.get(Calendar.MINUTE);
		return minutes;
	}
	
	public static int getTimeInHour(){
		Calendar c = Calendar.getInstance(); 
		int hours = c.get(Calendar.HOUR);
		return hours;
	}
	
	public static long getTimeInLong(){
		Time timeStamp = new Time();
		timeStamp.set(Calendar.getInstance().getTimeInMillis());
		return timeStamp.toMillis(true);
	}
	
	public static Time getTimeInTime(){
		Time timeStamp = new Time();
		timeStamp.set(Calendar.getInstance().getTimeInMillis());
		return timeStamp;
	}
	
	public static String getTimeInGMT(Time timeStamp){
		return (new Date(timeStamp.toMillis(true))).toGMTString();
	}
	
	public static String getTimeInLocale(Time timeStamp){
		return (new Date(timeStamp.toMillis(true))).toLocaleString();
	}
	
	public static String getTimeDifferenceInString(Time t1, Time t2){
		String timeLeft = (String) DateUtils.getRelativeTimeSpanString(t1.toMillis(true), t2.toMillis(true), 0);
		timeLeft = timeLeft.split(" ago")[0];
		return timeLeft;
	}
	
	public static String getTimeDifferenceInString(long t1, long t2){	// t1=time, t2=now
		String timeLeft = (String) DateUtils.getRelativeTimeSpanString(t1, t2, 0);
		
		if(t1<=t2){
			return "Overdue";
		}else{
			if((t1-t2)/3600/1000 <=24){
				return String.format("%.1f hrs", (t1-t2)/1000/3600.0);
			}else if((t1-t2)/1000/3600/24<31){
				return String.format("%.1f days", (t1-t2)/1000/3600/24.0);
			}else if(((t1-t2)/1000/3600/24/30)<12){
				return String.format("%.1f months", (t1-t2)/1000/3600/24/30.0);
			}else{
				return String.format("%.1f years", (t1-t2)/1000/3600/24/12/30.0);
			}
			
		}
//		
//		
//		Log.i("STOPWATCH", "timeLeft: "+timeLeft);
//		if(timeLeft.contains("in")){
//			timeLeft = timeLeft.split("in ")[1];
//			return timeLeft;
//		}else if(timeLeft.contains("ago")){
//			Log.i("STOPWATCH", "Overdue: ");
//			return "Overdue";
//		}else{
//			return "XXX";
//		}
//		
	}
}
